﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ADShop.Models;
namespace ADShop.Controllers
{
    public class DetailsProductController : Controller
    {
        //
        // GET: /DetailsProduct/
        #region "Variable"
        //ADShopEntities adshop = new ADShopEntities();
        EShopEntities1 adshop = new EShopEntities1();
        #endregion
        public ActionResult Index()
        {
            return View();
        }

        public  ActionResult Details(int id)
        {
            List<ImagesProduct> im = new List<ImagesProduct>();
            if (adshop.Product.SingleOrDefault(d => d.IDProduct == id).ProductImage != null)
            {
                im.Add(new ImagesProduct()
                    {
                        LinkImagesProduct = adshop.Product.SingleOrDefault(d => d.IDProduct == id).ProductImage,
                        DescriptionImages = adshop.Product.SingleOrDefault(d => d.IDProduct == id).NameProduct
                    });
            }
            im.Add(new ImagesProduct()
                {
                   LinkImagesProduct = "http://www.wwubap.org/wp-content/uploads/2012/03/no-available-image.png",
                   DescriptionImages = "No Image Aviable"
                });
           
            ViewBag.DetailsProduct = adshop.Product.Where(d => d.IDProduct == id).SingleOrDefault();
            ViewBag.DetailsProductImages = adshop.ImagesProduct.Any(d => d.IDProduct == id) ? adshop.ImagesProduct.Where(i => i.IDProduct == id).ToList() :  im ;
            adshop.Product.Where(d => d.IDProduct == id).SingleOrDefault().View += 1;
            adshop.SaveChanges();
            return View(adshop.Product.Where(s => s.IDCategory == adshop.Product.FirstOrDefault(cat => cat.IDProduct == id).IDCategory).ToList());
        }
    }
}
